/*
 * $Id: PictureViewer.java 189 2009-06-11 10:35:42Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.apps.pictureviewer.widget;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JComponent;

import deltadesk.apps.pictureviewer.data.Picture;
import deltadesk.apps.pictureviewer.data.PictureModel;
import deltadesk.apps.pictureviewer.util.GraphicsUtil;

/**
 * PictureViewer
 * 
 * @author Andreas Meyer
 * @version $Revision: 189 $
 */
public class PictureViewer extends JComponent {

	/** The serial version UID. */
	private static final long serialVersionUID = 2623806357013310697L;
	
	/** The model. */
	private PictureModel model;
	
	/** The AWT image. */
	private BufferedImage image;
	
	/** The current index in the model. */
	private int index;
	
	/** The flag for scaling up images tht are smaller than the screen. */
	private boolean scaleUp;


	/**
	 * Construct the viewer.
	 * 
	 */
	public PictureViewer(final PictureModel model) {
		
		this.model = model;
		setBackground(Color.BLACK);
		
		addComponentListener(new ComponentAdapter() {

			public void componentResized(final ComponentEvent ce) {

				updateImage();
			}
		});
	}

	/**
	 * @param index the index to set
	 */
	public void setIndex(final int index) {
		
		this.index = index;
		updateImage();
	}

	/**
	 * @return the scaleUp
	 */
	public boolean isScaleUp() {
		return scaleUp;
	}

	/**
	 * @param scaleUp the scaleUp to set
	 */
	public void setScaleUp(final boolean scaleUp) {
		
		this.scaleUp = scaleUp;
		repaint();
	}

	/**
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(final Graphics g) {

		Graphics2D g2 = (Graphics2D)g;
		
		g2.setColor(getBackground());
		g2.fillRect(0, 0, getWidth(), getHeight());

		if(image == null) {
			return;
		}

		final Rectangle rect = GraphicsUtil.fitRectangleInto(
				getWidth(),
				getHeight(), 
				image.getWidth(),
				image.getHeight(),
				scaleUp);
			
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.drawImage(image, rect.x, rect.y, rect.width, rect.height, null);
	}
	
	/**
	 * Update the image.
	 */
	private void updateImage() {
		
		// set the image
		try {
			final Picture pic = model.getPicture(index);
			image = pic.getImage();
		} catch (IOException e) {
			// TODO set error image
			e.printStackTrace();
		}
		repaint();
	}

}
